﻿using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using DataAccess;
using Entities;
namespace Business
{
    public class SupportMaterialBusiness
    {

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static SupportMaterialBusiness Instance { get; private set; }
        /// <summary>
        /// Initializes the <see cref="SupportMaterialBusiness" /> class.
        /// </summary>
        static SupportMaterialBusiness() { Instance = new SupportMaterialBusiness(); }
        /// <summary>
        /// Updates the specified supporting material.
        /// </summary>
        /// <param name="supportingMaterial">The supporting material.</param>
        /// <returns></returns>
        public string Update(SupportingMaterial supportingMaterial)
        {
            if (supportingMaterial == null)
            {

                return "supportingMaterial is not null";
            }
            try
            {
                return SupportingMaterialDataAccess.Instance.UpdateSupportingMaterial(supportingMaterial) > 0 ? "Update Successfull!" : "Update Fail";
            }
            catch (SqlException exception)
            {

                return exception.Message;
            }

        }
        /// <summary>
        /// Pagings the supporting material.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="lstMaterial">The LST material.</param>
        /// <returns></returns>
        public Collection<SupportingMaterial> PagingSupportingMaterial(int pageNumber, int pageSize, Collection<SupportingMaterial> lstMaterial)
        {

            if (lstMaterial != null)
            {
                if (pageSize > 0)
                {
                    var totalpage = lstMaterial.Count % pageSize == 0 ? lstMaterial.Count / pageSize : lstMaterial.Count / pageSize + 1;
                    if (pageNumber <= totalpage && pageNumber > 0)

                        return new Collection<SupportingMaterial>(lstMaterial.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());

                }
                return null;
            }
            return null;
        }
        /// <summary>
        /// Gets the support material.
        /// </summary>
        /// <param name="supportingMaterialId">The supporting material id.</param>
        /// <returns></returns>
        public SupportingMaterial GetSupportMaterial(string supportingMaterialId)
        {
            try
            {
                if (!string.IsNullOrEmpty(supportingMaterialId) && supportingMaterialId != Guid.Empty.ToString())
                    return SupportingMaterialDataAccess.Instance.GetSupportingMaterial(supportingMaterialId);
                return null;
            }
            catch (SqlException exception)
            {


                return null;
            }

        }
        /// <summary>
        /// Gets the support material.
        /// </summary>
        /// <param name="supportingMaterialId">The supporting material id.</param>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public SupportingMaterial GetSupportMaterial(string supportingMaterialId, out string message)
        {
            try
            {
                if (!string.IsNullOrEmpty(supportingMaterialId) && supportingMaterialId != Guid.Empty.ToString())
                {
                    message = "Getted"; return SupportingMaterialDataAccess.Instance.GetSupportingMaterial(supportingMaterialId);
                }
                message = "";
                return null;
            }
            catch (SqlException exception)
            {
                message = exception.Message;

                return null;
            }

        }
        /// <summary>
        /// Lists the support material.
        /// </summary>
        /// <param name="organisationId">The organisation id.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <returns></returns>
        public Collection<SupportingMaterial> ListSupportMaterial(string organisationId, bool isActive)
        {
            try
            {
                if (!string.IsNullOrEmpty(organisationId) && organisationId != Guid.Empty.ToString())
                    return SupportingMaterialDataAccess.Instance.ListSupportingMaterial(organisationId, isActive);
                return null;
            }
            catch (SqlException exception)
            {


                return null;
            }

        }
        /// <summary>
        /// Lists the support material.
        /// </summary>
        /// <param name="organisationId">The organisation id.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Collection<SupportingMaterial> ListSupportMaterial(string organisationId, bool isActive, out string message)
        {
            try
            {
                message = "";
                if (!string.IsNullOrEmpty(organisationId) && organisationId != Guid.Empty.ToString())
                    return SupportingMaterialDataAccess.Instance.ListSupportingMaterial(organisationId, isActive);
                return null;
            }
            catch (SqlException exception)
            {
                message = exception.Message;
                return null;
            }

        }
    }
}
